package com.hulu.yec.sys.course.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.filters.CorsFilter;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hulu.yec.common.Constant;
import com.hulu.yec.common.PageModel;
import com.hulu.yec.common.exception.ControllerRuntimeException;
import com.hulu.yec.sys.course.entity.CourseEntity;
import com.hulu.yec.sys.course.service.CourseDao;

/**
 * CourseServlet
 */
@WebServlet("/course/CourseServlet")
public class CourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码格式
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		// new一个courseDao调用业务逻辑
		CourseDao courseDao = new CourseDao();
		// 获取前台传来的参数调用方法
		String command = request.getParameter("command");
		try {
			if ("courseList".equals(command)) {
					courseList(request, response, courseDao);
			} else if ("courseInItInsert".equals(command)) {
				courseInItInsert(request	,	response , courseDao);
			}else if ("courseInsert".equals(command)){
				courseInsert(request	,	response , courseDao);
			}else if("courseIntiUpdate".equals(command)){
				courseIntiUpdate(request	,	response	,	courseDao);
			}else if("courseUpdate".equals(command)){
				courseUpdate(request	,	response , courseDao);
			}else if("courseDelete".equals(command)){
				courseDelete(request	,	response , courseDao);
			}
		} catch (ControllerRuntimeException e) {
			e.printStackTrace();
		}
	}




	/**
	 * 查询所有的课程（表格）
	 * 
	 * @param request
	 * @param response
	 * @param courseDao2
	 * @param courseDao
	 * @throws ControllerRuntimeException
	 */

	private void courseList(HttpServletRequest request, HttpServletResponse response, CourseDao courseDao)
			throws ControllerRuntimeException {
		// 接收参数
		Integer pageSize = 5;//Constant.DEFAULT_PAGE_SIZE;
		Integer pageNo = Constant.DEFAULT_PAGE_NO;

		String pageNoStr = request.getParameter("pageNo");
		String pageSizeStr = request.getParameter("pageSize");

		if (StringUtils.isNoneBlank(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		if (StringUtils.isNoneBlank(pageSizeStr)) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		
		//查询参数
		int state = 0;
		String name = request.getParameter("submitName");
		String operateUserId = request.getParameter("submitOperateUserId");
		String stateStr = request.getParameter("SubmitState");
		
		if (StringUtils.isNotBlank(stateStr)){
			state = Integer.parseInt(stateStr);
		}
		//创建一个实体把查询条件参数传入Dao层
		CourseEntity courseEntity = new CourseEntity();
		courseEntity.setName(name);
		courseEntity.setOperateUserId(operateUserId);
		courseEntity.setState(state);

		// 调用业务逻辑
		PageModel<CourseEntity> pageModel = courseDao.courseList(courseEntity,pageNo, pageSize);
		//获取pageModel中所有List集合
		List<CourseEntity> Course= pageModel .getList();
		//取得pageModel中所有List集合中的pId
		List<Integer> ids=new ArrayList<Integer>();
		for(int i=0;i< Course.size();i++){
			 ids.add(Integer.parseInt(Course.get(i).getPId()));
		}
		//把对应的pId的名字赋给pId
		List<CourseEntity> courses=courseDao.getName( ids);
		for(int i=0;i< pageModel.getList().size();i++){
			 pageModel.getList().get(i).setPId(courses.get(i).getName());
		}
		List<CourseEntity> childAllList = courseDao.courseAllMenuList();
		String json = JSONObject.toJSONString(childAllList);
		// 转向
		try {
		
			request.setAttribute("pageModel", pageModel);
			request.setAttribute("courseEntity", courseEntity);
			request.setAttribute("json", json);
			
			request.getRequestDispatcher("/WEB-INF/view/sys/course/course-list.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 新增课程（1）跳转到新增页面--->>>以后要做--->>>判断当前操作者Id，是否拥有权限添加课程
	 * 
	 * @param request
	 * @param response
	 * @param courseDao
	 */
	private void courseInItInsert(HttpServletRequest request,
			HttpServletResponse response, CourseDao courseDao) {
			//-------------1、前台传来参数--------------//
			//-------------2、调用业务逻辑--------------//
			//查询所有课程pId
			List<CourseEntity> pIdList =courseDao.pIdList();
			//-------------3、转			    向--------------//
			try {
				request.setAttribute("pIdList", pIdList);
				request.getRequestDispatcher("/WEB-INF/view/sys/course/course-add.jsp").forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	
	/**
	 * 新增课程（2）
	 * 
	 * @param request
	 * @param response
	 * @param courseDao
	 */
	private void courseInsert(HttpServletRequest request,
			HttpServletResponse response, CourseDao courseDao) {
			//-------------1、前台传来参数--------------//
			//定义一个变量，来判断是否增加成功
			Integer row = 0;
			String pId 					= request.getParameter("pId");
			String name 				= request.getParameter("name");
			Integer isLeaf 				= Integer.valueOf(request.getParameter("isLeaf"));
			String memo 				= request.getParameter("memo"); 
			Integer state 				= Integer.valueOf(request.getParameter("state"));
			//-------------2、调用业务逻辑--------------//
			//把获取到的参数封装成一个实体对象
			CourseEntity courseInsert =new CourseEntity();
			courseInsert.setPId(pId);
			courseInsert.setName(name);
			courseInsert.setIsLeaf(isLeaf);
			courseInsert.setState(state);
			courseInsert.setMemo(memo);
			row = courseDao.addCourse(courseInsert);
			//-------------3、转			    向--------------//
			try {
				if(row >0 ){
					request.getRequestDispatcher("/course/CourseServlet?command=courseList").forward(request, response);
				}else{
					request.getRequestDispatcher("/WEB-INF/view/sys/course/course-list.jsp").forward(request, response);
				}
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}


	/**
	 * 修改课程表（1）查询出课程表对应id的课程的详细信息
	 * 
	 * @param request
	 * @param response
	 * @param courseDao
	 */
	private void courseIntiUpdate(HttpServletRequest request,
			HttpServletResponse response, CourseDao courseDao) {
		//-------------1、前台传来参数--------------//
		Integer id  		= Integer.parseInt(request.getParameter("id"));
		//-------------2、调用业务逻辑--------------//
		CourseEntity courseIntiUpdateList = courseDao.courseIntiUpdateList(id);
		//-------------3、转			    向--------------//
		try {
				request.setAttribute("courseIntiUpdateList", courseIntiUpdateList);
				request.getRequestDispatcher("/WEB-INF/view/sys/course/course-update.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 修改课程表（2）修改课程表对应id的课程信息
	 * @param request
	 * @param response
	 * @param courseDao
	 */
	private void courseUpdate(HttpServletRequest request,
			HttpServletResponse response, CourseDao courseDao) {
			//-------------1、前台传来参数--------------//
			Integer 	id							=		Integer.parseInt(request.getParameter("id"));
			String		pId						= 	request.getParameter("pId");
			Integer 	isLeaf					=		Integer.parseInt(request.getParameter("isLeaf"));
			String		name					=		request.getParameter("name");
			String 		operateUserId		=		request.getParameter("operateUserId");
			Integer		state						=		Integer.parseInt(request.getParameter("state"));
			String		memo					=		request.getParameter("memo");
			//-------------2、调用业务逻辑--------------//
			//把参数封装到实体中传入Dao层
			CourseEntity	course 		= 	new CourseEntity();
			course.setPId(pId);
			course.setIsLeaf(isLeaf);
			course.setName(name);
			course.setOperateUserId(operateUserId);
			course.setState(state);
			course.setMemo(memo);
			//调用Dao层
			int row = courseDao.courseUpdate(course,id);
			//-------------3、转			    向--------------//
			try {
					if(row >0 ){
						request.getRequestDispatcher("/course/CourseServlet?command=courseList").forward(request, response);
					}else{
						request.getRequestDispatcher("/WEB-INF/view/sys/course/course-list.jsp").forward(request, response);
					}
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	
	/**
	 * 删除课程表中的对应id的基本信息
	 * @param request
	 * @param response
	 * @param courseDao
	 */
	private void courseDelete(HttpServletRequest request,
			HttpServletResponse response, CourseDao courseDao) {
		//-------------1、前台传来参数--------------//
		Integer id 		= 	Integer.parseInt(request.getParameter("id"));
		//-------------2、调用业务逻辑--------------//
		int row = courseDao.courseDelete(id);
		//-------------3、转			    向--------------//
		try {
			if(row >0 ){
					request.getRequestDispatcher("/course/CourseServlet?command=courseList").forward(request, response);
				}else{
					request.getRequestDispatcher("/WEB-INF/view/sys/course/course-list.jsp").forward(request, response);
				}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	
	
	
}
